package com.icss.th.subjects.service;

import java.util.List;
import java.util.Map;

import com.icss.th.common.FacadeFactory;
import com.icss.th.subjects.facade.ISubjectsFacade;
import com.icss.th.subjects.pojo.Subject;

public class SubjectService 
{
	private static ISubjectsFacade subjectsFacade = (ISubjectsFacade) FacadeFactory.getInstance(ISubjectsFacade.class);
	
	private static Subject root = getRootSubject();
	
	public static Map<String, String> KEY_TYPE_MAP;

	public static String getSubjectName(String id)
	{
		if (KEY_TYPE_MAP != null)
		{
			return KEY_TYPE_MAP.get(id);
		}
		
		return "";
	}

	public static Subject getRoot() 
	{
		return root;
	}
	
	public static List<Subject> getPopular(int num)
	{
		return subjectsFacade.getPopular(num);
	}

	private static Subject getRootSubject() 
	{
		Subject root = new Subject(0, "root", 0, null);
		initSubject(root, 2);
		
		return root;
	}
	
	/**
	 * 以传入类别为根、以level级别为叶子节点生成树
	 * @param subject
	 * @param conn
	 * @param level
	 */
	private static void initSubject(Subject subject, int level) 
	{
		if (subject == null || subject.getLevel() == level) return ;
		
		List<Subject> children = subjectsFacade.getChildren(subject);
		subject.setChildren(children);
		
		if (children != null && !children.isEmpty())
		{
			for (Subject child : children)
			{
				initSubject(child, level);
			}
		}
	}
}
